package com.jsg.sl651.handler;

import com.alibaba.fastjson.JSONObject;
import com.jsg.sl651.boot.BootProperties;
import com.jsg.sl651.entity.data.Element;
import com.jsg.sl651.entity.frame.FrameMessageWrapper;
import com.jsg.sl651.handler.mqtt.MQTTConnect;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import org.apache.commons.codec.binary.Hex;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.jetlinks.core.message.DeviceOnlineMessage;
import org.jetlinks.core.message.property.ReportPropertyMessage;
import org.jetlinks.protocol.official.binary.BinaryDeviceOnlineMessage;
import org.jetlinks.protocol.official.binary.BinaryMessageType;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class SL651ToJetLinksByMQTT {
    private Socket socket;
    private static InputStream inputStream;
    private static OutputStream outputStream;

    public SL651ToJetLinksByMQTT() {
        this.socket = null;
    }

    public void toJetLinks(FrameMessageWrapper frameMessageWrapper) throws Exception {
        System.out.println("Received sendData: Start");
        BootProperties bootProperties = new BootProperties();
        String productId = bootProperties.getProductId();
        String deviceId = frameMessageWrapper.getHeader().getRemoteTelemetryStationAddress().getMessageHex();
        MQTTConnect mqttConnect = new MQTTConnect(bootProperties.getIotHostMQTT(),productId,deviceId);
        mqttConnect.start(bootProperties.getSecureId(), bootProperties.getSecureKey(),30, false,true,60);
        mqttConnect.sub();
        if(!frameMessageWrapper.getBody().getDataElements().isEmpty()){
            Map<String, Object> messageMap = new HashMap<String, Object>();
            Map<String, Object> properties = new HashMap<String, Object>();
            for (Element<?> dataElement : frameMessageWrapper.getBody().getDataElements()) {
                System.out.println(dataElement.getHexKey() + "===" + dataElement.getData() + "===" + dataElement.getName());
                if(!dataElement.getName().equals("未知属性") && !dataElement.getName().equals("遥测站状态及报警信息")
                && !dataElement.getName().equals("测站编码引导符")){
                    if(dataElement.getName().equals("观测时间引导符")){
                        String dateTime = dataElement.getData().toString().replace("T"," ");
                        properties.put(dataElement.getHexKey() + "H", dateTime);
                    }else{
                        properties.put(dataElement.getHexKey() + "H", dataElement.getData());
                    }
                }
            }
            messageMap.put("properties", properties);
            messageMap.put("deviceId", deviceId);
//            messageMap.put("messageId", 65535);
            JSONObject jsonObject1 = new JSONObject(messageMap);
            mqttConnect.pub(jsonObject1.toString(),0);
        }

        System.out.println("Received sendData: Success");
    }

}
